package app.android.kdictionary.Util;


import java.net.URLEncoder;
import java.util.ArrayList;

import app.android.kdictionary.aidl.WebViewContent;

public class HttpParser {
	private String cssString = "<style type=\"text/css\">\n"+
	"\n"+
	"ul,ol,dl,div,h1,h2,h3 {margin:0px;padding:0px;}\n"+
	"p {line-height:1.4;}\n"+
	".b {font-weight:bold;}\n"+
	".sp {letter-spacing:-1;}\n"+
	".b0 {border:0px; background:transparent;}\n"+
	".vM {vertical-align:middle;}\n"+
	"\n"+
	".aR {text-align:right}\n"+
	".aL {text-align:left}\n"+
	"\n"+
	".t11 {font-size:11px; font-family:dotum}\n"+
	".t12 {font-size:12px;}\n"+
	".t13 {font-size:13px;}\n"+
	".t14 {font-size:14px;}\n"+
	".tb12 {font-size:12px; font-weight:bold; color:#262626 }\n"+
	"\n"+
	".p15_5 {padding:15px 5px 5px 15px}\n"+
	"\n"+
	".gray01 {color:#737373}\n"+
	"\n"+
	".sword {font-family:New gulim, \"새굴림\", AppleGothic, Sans-serif ; font-size:14px; font-weight:bold; color:#336699}\n"+
	".provtitle {font-family:New gulim; font-size:13px; font-weight:bold; color:#000000;padding:20px 10px 10px 10px}\n"+
	".exp {font-family:New gulim, \"새굴림\", AppleGothic, Sans-serif;color:#000000; line-height:1.5; padding:5px; }\n"+
	".sdblue {font-family:New gulim, \"새굴림\", AppleGothic, Sans-serif; font-size:13px;color:#336699;}\n"+
	".NumRG {font-family:tahoma; font-weight:bold;color:#549606; padding-top:-10px; margin-bottom:-0.1;vertical-align:top}\n"+
	".NumRG2 {font-family:tahoma;  font-weight:bold;color:#336699;padding-left:8px; margin-top:-10;vertical-align:top;}\n"+
	".NumNO {font-family:New gulim; width:45px; font-weight:bold;color:#cb4a00;vertical-align:top; text-align:right}\n"+
	".Use_icon {color:#444444; padding-left:20px;vertical-align:top }\n"+
	".Use { color:#444444;}\n"+
	".Use2 {color:#444444;}\n"+
	"\n"+
	".prov {background:url(http://124.137.201.223/image/icon_prov.gif) no-repeat; background-position:10px 5px; padding:5px 0px 5px 40px;line-height:1.3 }\n"+
	".idiom {background:url(http://124.137.201.223/image/icon_idiom.gif) no-repeat;background-position:10px 5px;  padding:5px 0px 5px 40px; line-height:1.3}\n"+
	"\n"+
	".list2 {padding-left:20px}\n"+
	".word_title { font-family:New gulim, \"새굴림\", AppleGothic, Sans-serif ; font-weight:bold; font-size:14px; color:#006699 }"+
	"</style>\n";

	public boolean parseContent(String data, WebViewContent content) {
		String contentRegion = "class=\"list\"";
		String titleRegion = "class=\"word_title\"";
		String titleStr;
		String tmpStr;
		int startIdx, endIdx;
		
		if(data.indexOf(contentRegion) == -1)
			return false;
		
		startIdx = data.indexOf(titleRegion) + titleRegion.length()+1;
		endIdx = startIdx + data.substring(startIdx).indexOf("</span>");
		titleStr = data.substring(startIdx, endIdx);
		if(titleStr.indexOf("<img") != -1) {
			startIdx = titleStr.indexOf("<img");
			endIdx = startIdx + titleStr.substring(startIdx).indexOf(">");
			titleStr = titleStr.substring(0, startIdx) + titleStr.substring(endIdx+1);
		}
		titleStr = "<strong>"+titleStr+"</strong><hr><br/>";
		
		startIdx = data.substring(0, data.indexOf(contentRegion) ).lastIndexOf("<");
		endIdx = data.lastIndexOf("</span>");
		tmpStr = data.substring(startIdx, endIdx);
		
		
		
		content.setWebViewString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">"
				+cssString
				+(titleStr+tmpStr).replaceAll("%", URLEncoder.encode("%")));
		return true;
	}

	public boolean parseSimpleList(String data, ArrayList<WebViewContent> list) throws Exception{
		String tmpStr;
		String[] dataList;
		int startIdx, endIdx;
		
		if(data.indexOf("\"exp\"") == -1)
			return false;
		startIdx = data.substring(0, data.indexOf("\"exp\"") ).lastIndexOf("<");
		endIdx = data.lastIndexOf("</p>")+4;
		tmpStr = data.substring(startIdx, endIdx);
		dataList = tmpStr.split("</p>");
		
		WebViewContent wView = null;
		
		for(int i = 0 ; i < dataList.length ; i++) {
			
			tmpStr = dataList[i]+"</p>";
		
			try {
				wView = new WebViewContent();
				parseSimpleData(tmpStr, wView);
				list.add(wView);
				System.out.println(wView.getWebViewString());
			} catch(Exception e) {
			}
		}
		return true;
	}

	public boolean parseDeepList(String data, ArrayList<WebViewContent> list) throws Exception{
		String tmpStr;
		String[] dataList;
		int startIdx, endIdx;
		
		if(data.indexOf("\"exp\"") == -1)
			return false;
		startIdx = data.substring(0, data.indexOf("\"exp\"") ).lastIndexOf("<");
		endIdx = data.lastIndexOf("</p>")+4;
		tmpStr = data.substring(startIdx, endIdx);
		dataList = tmpStr.split("</p>");
		
		WebViewContent wView = null;
		
		for(int i = 0 ; i < dataList.length ; i++) {
			
			tmpStr = dataList[i]+"</p>";
			
			try {
				wView = new WebViewContent();
				parseSimpleData(tmpStr, wView);
				list.add(wView);
			} catch(Exception e) {
			}
		}
		return true;
	}

	
	public boolean parseSectionList(String data, ArrayList<WebViewContent> list) throws Exception{
		String tmpStr;
		String[] dataList;
		int startIdx, endIdx;
		
		if(data.indexOf("\"exp\"") == -1)
			return false;
		startIdx = data.substring(0, data.indexOf("\"exp\"") ).lastIndexOf("<");
		endIdx = data.lastIndexOf("</p>")+4;
		tmpStr = data.substring(startIdx, endIdx);
		dataList = tmpStr.split("</p>");
		
		WebViewContent wView = null;
		
		for(int i = 0 ; i < dataList.length ; i++) {
			
			tmpStr = dataList[i]+"</p>";
			
			try {
				wView = new WebViewContent();
				parseSectionData(tmpStr, wView);
				list.add(wView);
			} catch(Exception e) {
			}
		}
		return true;
	}

	private void parseSimpleData(String data, WebViewContent content) throws Exception{
		String preStr;
		String tmpStr;
		String linkStr;
		
		preStr = data.substring(0, data.indexOf("<a href"));
		tmpStr = data.substring(data.indexOf("<a href"));
		linkStr = tmpStr.substring(0, tmpStr.indexOf(">")+1);
		tmpStr = tmpStr.substring(tmpStr.indexOf(">")+1);
		tmpStr = tmpStr.replaceAll("</a>", "");
		
		content.setIdx(Integer.parseInt(linkStr.split("','")[1]));
		content.setWebViewString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><!-- "+content.getIdx()+"-->"
				+cssString
				+(preStr+tmpStr).replaceAll("%", URLEncoder.encode("%")));
	}

	private void parseSectionData(String data, WebViewContent content) throws Exception{
		String preStr;
		String tmpStr;
		String linkStr;
		
		preStr = data.substring(0, data.indexOf("<a href"));
		tmpStr = data.substring(data.indexOf("<a href"));
		linkStr = tmpStr.substring(0, tmpStr.indexOf(">")+1);
		tmpStr = tmpStr.substring(tmpStr.indexOf(">")+1);
		tmpStr = tmpStr.replaceAll("</a>", "");

		content.setIdx(Integer.parseInt(linkStr.split("','")[1]));
		content.setWebViewString("<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"><!-- "+content.getIdx()+"-->"
				+cssString
				+(preStr+tmpStr).replaceAll("%", URLEncoder.encode("%")));
	}
	
	public String getProverbCategoryAmount(String data) {
		int startIdx = data.indexOf("<div class=\"result_title\">");
		int endIdx = startIdx + data.substring(startIdx).indexOf("</div>");
		String tmpStr = data.substring(startIdx, endIdx);
		return tmpStr.substring(tmpStr.lastIndexOf("(")+1, tmpStr.lastIndexOf(")")-1);
	}
	
	public String getSectionAmount(String data) {
		int startIdx = data.indexOf("<div class=\"result_title\">");
		int endIdx = startIdx + data.substring(startIdx).indexOf("</div>");
		String tmpStr = data.substring(startIdx, endIdx);
		return tmpStr.substring(tmpStr.lastIndexOf("(")+1, tmpStr.lastIndexOf(")")-1);
	}

	public String getSimpleAmount(String data) {
		int startIdx = data.indexOf("class=\"sword\"");
		
		String tmpStr = data.substring(startIdx);
		tmpStr = tmpStr.substring(0, tmpStr.indexOf("</span>"));
		return tmpStr.substring(tmpStr.lastIndexOf("(")+1, tmpStr.lastIndexOf(")")-1);
	}

}
